﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Future;

namespace Future.ConsoleTest
{
    public class WcfClientInsert
    {
        Future.WcfClient.ClientProcess[] _clients;
        Thread[] _threads;
        int _testNum = 1;
        Timer _timeLog;
        int _logSpan = 10 * 1000;

        public WcfClientInsert()
        {
            _timeLog = new Timer(new TimerCallback(PrintLog), null, System.Threading.Timeout.Infinite, _logSpan);
            _clients = new Future.WcfClient.ClientProcess[_testNum];
            _threads = new Thread[_testNum];

            for (int i = 0; i < _testNum; i++)
            {
                WcfClient.ClientProcess tempClient = new WcfClient.ClientProcess();
                _clients[i] = tempClient;

                Thread tempThread = new Thread(new ParameterizedThreadStart(Insert));
                tempThread.IsBackground = true;
                _threads[i] = tempThread;
            }
        }

        public bool Start()
        {
            bool bRet = false;
            try
            {
                _timeLog.Change(0, _logSpan);
                for (int i = 0; i < _testNum; i++)
                {
                    _threads[i].Start(i);
                }
                bRet = true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                bRet = false;
            }
            return bRet;
        }

        private void Insert(object index)
        {
            int i = int.Parse(index.ToString());
            while (true)
            {
                Channel.Task task = new Channel.Task();
                task.CommandName = Contract.TaskCommandEnums.Insert;
                Entities.xHeadEntity headEntity = new Entities.xHeadEntity();
                headEntity.sName = "Test" + i;
                headEntity.iEnable = false;
                headEntity.dDate = DateTime.Now;
                headEntity.iMoney = 100.8;

                List<Contract.BaseEntity> childEntity = new List<Contract.BaseEntity>();
                for (int n = 0; n < 3; n++)
                {
                    Entities.xDetailEntity detailEntity = new Entities.xDetailEntity();
                    detailEntity.sDetailName = "Detail Test";
                    detailEntity.iDetailEnable = true;
                    detailEntity.iDetailMoney = 1.88;
                    detailEntity.dDetailDate = DateTime.Now;
                    childEntity.Add(detailEntity);
                }
                headEntity.ChildItem = childEntity;

                task.BufferArgument = Common.ZipTools.CompressionObject<Entities.xHeadEntity>(headEntity);
            }
        }

        private void PrintLog(object obj)
        {

        }
    }
}
